package com.example.demo.controller;

import com.example.demo.entity.Coach;
import com.example.demo.entity.Licence;
import com.example.demo.entity.Response;
import com.example.demo.mapper.LicenceMapper;
import com.example.demo.service.Impl.LicenceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static java.time.LocalDateTime.now;
import static java.util.Map.of;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequestMapping("/licence")
@RequiredArgsConstructor
public class LicenceController {
    private final LicenceImpl licenceImpl;
    @GetMapping("/findAllLicence")
    public ResponseEntity<Response> findAllLicence() {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("getAllLicence",licenceImpl.findAllLicence()))
                        .message("获取所有驾照信息")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    @GetMapping("/findHotLicence")
    public ResponseEntity<Response> findHotLicence() {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("findHotLicence",licenceImpl.findHotLicence()))
                        .message("驾照类型火爆排行！！！")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    @PostMapping("/addLicence")
    public ResponseEntity<Response> addLicence(@RequestBody Licence licence) {
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("addLicence",licenceImpl.addLicence(licence)))
                            .message("添加驾照")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("添加驾照失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
    @DeleteMapping("/deleteLicence")
    public ResponseEntity<Response> deleteLicence(@RequestParam Long licenceId) {
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("deleteLicence",licenceImpl.deleteLicence(licenceId)))
                            .message("删除驾照")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("删除驾照失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
    @PostMapping("/updateLicence")
    public ResponseEntity<Response> updateLicence(@RequestBody Licence licence) {
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("updateLicence",licenceImpl.updateLicence(licence)))
                            .message("更新驾照")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("更新驾照失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
}
